// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef CLOUDAPPENGINE_CAEWSSSOCKET_H
#define CLOUDAPPENGINE_CAEWSSSOCKET_H
#include "CaeWebSocket.h"
#include "CaeTlsSocket.h"

class CaeWssSocket : public CaeWebSocket {

public:
    explicit CaeWssSocket(CaeTlsSocket *socket);

    ~CaeWssSocket() override;

    int GetFd() override;

    void SetSocketOption(uint32_t socketOption) override;

    int Recv(void *buf, size_t len) override;

    int Send(void *uBuf, size_t len) override;

    int Send(void *uBuf, size_t len, enum WB_OPCODE wp) override;

    bool Handshake() override;

    void CloseSocket() override;

    int GetSocketOption() override
    {
        if (m_tlsSocket != nullptr) {
            return m_tlsSocket->GetSocketOption();
        } else {
            return 0;
        }
    }

    void SetFd(int fd) override
    {
        if (m_tlsSocket != nullptr) {
            m_tlsSocket->SetFd(fd);
        }
    }

    void SetStatus(int status) override
    {
        if (m_tlsSocket != nullptr) {
            m_tlsSocket->SetStatus(status);
        }
    }

    int GetStatus() override
    {
        if (m_tlsSocket != nullptr) {
            return m_tlsSocket->GetStatus();
        } else {
            return -1;
        }
    }

    void SetSocketTimeout(long sec, long usec) override
    {
        if (m_tlsSocket != nullptr) {
            m_tlsSocket->SetSocketTimeout(sec, usec);
        }
    }

private:
    CaeTlsSocket *m_tlsSocket;

    bool ParseHttpHeader() override;
};
#endif // CLOUDAPPENGINE_CAEWSSSOCKET_H
